﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using Microsoft.Office.Interop.Excel;

namespace QUANLYDIEMHOCSINH
{
    public partial class xemxeploaihs : Form
    {
        LOPBUS lopbus = new LOPBUS();
        NAMHOCBUS nambus = new NAMHOCBUS();
        HOCKYBUS hkbus = new HOCKYBUS();
        DIEMBUS diembus = new DIEMBUS();
        public xemxeploaihs()
        {
            InitializeComponent();
            LoadDanhSachLop();
            LoadNamHoc();
            LoadDanhSachHocKy();
            LoadDanhSachXepLoai(int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbLop.SelectedValue.ToString()));
        }

        void LoadDanhSachLop()
        {
            cbLop.DataSource = lopbus.layDSL();
            cbLop.DisplayMember = "TENLOP";
            cbLop.ValueMember = "MALOP";
            cbLop.SelectedItem = 0;
        }

        void LoadNamHoc()
        {
            NAMHOCDTO namhoc = nambus.LoadNamHocHienTai();
            txtNamHoc.Text = namhoc.NAMBATDAU + " - " + namhoc.NAMKETTHUC;
        }
        void LoadDanhSachHocKy()
        {
            cbHocKy.DataSource = hkbus.LoadDachHocKy();
            cbHocKy.DisplayMember = "TENHK";
            cbHocKy.ValueMember = "MAHK";
            cbHocKy.SelectedIndex = 0;
        }
        void LoadDanhSachXepLoai(int mahk, int malop)
        {
            dtgrwBangXepLoai.DataSource = diembus.DanhSachXepLoaiHocSinh(mahk, malop);
            for (int i = 0; i < dtgrwBangXepLoai.Rows.Count;i++ )
            {
                dtgrwBangXepLoai.Rows[i].Cells[0].Value = (i + 1).ToString();
            }
            dtgrwBangXepLoai.Refresh();
        }
        private void cbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDanhSachXepLoai(int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbLop.SelectedValue.ToString()));
        }

        private void cbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDanhSachXepLoai(int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbLop.SelectedValue.ToString()));
        }

        private void btinbangxeploai_Click(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            Microsoft.Office.Interop.Excel.Application la = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = la.Workbooks.Add(XlSheetType.xlWorksheet);
            Worksheet ws = (Worksheet)la.ActiveSheet;

            la.Visible = true;

            ws.Cells[1, 1] = "Lớp :" + cbLop.Text;
            ws.Cells[1, 2] = "Học kì :" + cbHocKy.Text;
            ws.Cells[1, 3] = "Năm học :" + txtNamHoc.Text;



            ws.Cells[3, 1] = "STT";
            ws.Cells[3, 2] = "Họ Tên";

            ws.Cells[3, 3] = "Điểm TB";
            ws.Cells[3, 4] = "Xếp loại";
         
            int i = 4;
            for (int j = 0; j < dtgrwBangXepLoai.RowCount; j++)
            {
                ws.Cells[i, 1] = j+1; 
                ws.Cells[i, 2] = dtgrwBangXepLoai.Rows[j].Cells[1].Value;
                ws.Cells[i, 3] = dtgrwBangXepLoai.Rows[j].Cells[2].Value;
                ws.Cells[i, 4] = dtgrwBangXepLoai.Rows[j].Cells[3].Value;
                i++;
            }
           
           
        }
    }
}
